package com.iot.core.redis;

import com.google.common.collect.Sets;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;
import java.util.Set;

public class RedisConfig {

    private static final String REDIS_CONFIG_PROPERTIES = "/redis_config.properties";

    public static String password = "";

    public static Set<String> jedisClusterNodes = Sets.newHashSet();
    public static String redisNodes = "";
    public static GenericObjectPoolConfig genericObjectPoolConfig;
    public static int connectionTimeout = 2000;
    public static int soTimeout = 3000;
    public static int maxRedirections = 5;

    public static Properties props = new Properties();

    static {
        try {
            props.load(RedisConfig.class.getResourceAsStream(REDIS_CONFIG_PROPERTIES));
            password = props.get("redis.password").toString();
            jedisClusterNodes.addAll(Arrays.asList(props.get("redis.cluster.nodes").toString().split(",")));
            redisNodes = props.get("redis.cluster.nodes").toString();
            connectionTimeout = Integer.parseInt(props.get("redis.connection.timeout").toString());
            soTimeout = Integer.parseInt(props.get("redis.so.timeout").toString());
            maxRedirections = Integer.parseInt(props.get("redis.max.redirections").toString());

            genericObjectPoolConfig = new GenericObjectPoolConfig();
            genericObjectPoolConfig.setMaxIdle(Integer.parseInt(props.get("redis.pool.max-idle").toString()));
            genericObjectPoolConfig.setMaxTotal(Integer.parseInt(props.get("redis.pool.max-active").toString()));
            genericObjectPoolConfig.setMinIdle(Integer.parseInt(props.get("redis.pool.min-idle").toString()));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
